<?php

namespace app\model\v1\recovery;

use crmeb\traits\ModelTrait;
use think\facade\Db;
use think\Model;
use traits\model\SoftDelete;


class RecoveryQuestion extends Model
{
    use ModelTrait;

    protected $updateTime = false;
    protected $createTime = false;

    protected $hidden = [
    //    'create_time','update_time','delete_time'
    ];


    protected $table = 'eb_recovery_question';



    /**
     * 问题分类管理列表
     * @param $param
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     * @throws \think\exception\DbException
     */
    public static function getQuestionList($param = [])
    {
        $order = ["a.id"=>"desc"];

        $data['data'] = self::alias("a")
            ->field ("a.id,a.uid,a.desc,a.cate_id,a.img,a.status,FROM_UNIXTIME(a.create_time) as create_time,b.cate_name,c.nickname,c.phone")
            ->join ('recovery_question_cate b','a.cate_id=b.id','left')
            ->join ('user c','a.uid=c.uid','left')
            ->where(function ($query) use ($param){
                $query->whereNull("a.delete_time");
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('a.create_time', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
                //根据用户昵称/联系方式搜索
                if(!empty($param["word"])) $query->where("c.nickname",'like', '%'.$param["word"].'%')->whereOr("c.phone",'like', '%'.$param["word"].'%');
            })
            ->order($order)
            ->page($param["page"], $param['limit'])
            ->select()->toArray();

        $data['count'] = self::alias("a")
            ->field ("a.id,a.uid,a.desc,a.cate_id,a.img,a.status,a.create_time,b.cate_name,c.nickname,c.phone")
            ->join ('recovery_question_cate b','a.cate_id=b.id','left')
            ->join ('user c','a.uid=c.uid','left')
            ->where(function ($query) use ($param){
                $query->whereNull("a.delete_time");
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('a.create_time', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
                //根据用户昵称/联系方式搜索
                if(!empty($param["word"])) $query->where("c.nickname",'like', '%'.$param["word"].'%')->whereOr("c.phone",'like', '%'.$param["word"].'%');
            })
            ->count();

        return $data;
    }


    public static function getQuestionDetail($param = [])
    {
        $data = self::alias("a")
            ->field ("a.id,a.uid,a.desc,a.cate_id,a.img,a.status,a.create_time,b.cate_name,c.nickname,c.phone")
            ->join ('recovery_question_cate b','a.cate_id=b.id','left')
            ->join ('user c','a.uid=c.uid','left')
            ->where(function ($query) use ($param){
                $query->whereNull("a.delete_time");
                $query->where("a.id", $param["id"]);
            })
            ->find();

        return $data;
    }



    public static function enableQuestionDetail($param = [])
    {
        //获取用户发送的id，根据状态修改当前banner的启用禁用状态
        $status = self::where("id", $param['id'])->value("status");
        $msg = '';
        if($status == 1){   //1启用 2禁用
            $enable = 2;
            $msg = "禁用成功";
        }else{
            $enable = 1;
            $msg = "启用成功";
        }
        self::where("id", $param['id'])->update(['status' => $enable]);
        return $msg;
    }







}